package com.heima.wemedia.intecptor;

import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.thredlocal.WmediaThredLocal;
import lombok.val;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class WmediaInceptor implements HandlerInterceptor {

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        WmediaThredLocal.removeUserId();
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String userId = request.getHeader("userId");
        if(userId != null){
            WmUser user = new WmUser();
            user.setApUserId(Integer.valueOf(userId));
            WmediaThredLocal.setUserId(user);
        }
        return true;
    }
}
